﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace HuntTheWumpus
{

	public struct HighScoreStruct
	{
		public string Name;
		public int Score;
		public int CaveNumber;
	}
	class HighScore
	{
		public HighScore()
		{
			ReadHighscoreData();
		}
		List<HighScoreStruct> highscore = new List<HighScoreStruct>();
		public void ReadHighscoreData()
		{
			TextReader tr = new StreamReader("HighscoreData.txt");

			string input = tr.ReadLine();

			while (input != null)
			{
				string[] data = input.Split(',');
				HighScoreStruct hs;
				hs.Name = data[0];
				hs.Score = int.Parse(data[1]);
				hs.CaveNumber = int.Parse(data[2]);

				highscore.Add(hs);
				

				input = tr.ReadLine();
			}

			tr.Close();
		}
		public void WriteHighscoreData()
		{
			TextWriter tw = new StreamWriter("HighscoreData.txt");

			foreach (HighScoreStruct hss in highscore)
			{
				string output = hss.Name + "," +
								hss.Score + "," +
								hss.CaveNumber;
				tw.WriteLine(output);
			}
			tw.Close();
		}
		public void AddNewHighScore(HighScoreStruct hss)
		{
			highscore.Add(hss);

			highscore.Sort((HighScoreStruct s1, HighScoreStruct s2) => s2.Score.CompareTo(s1.Score));

			if (highscore.Count > 10)
			{
				highscore.RemoveAt(10);
			}
			WriteHighscoreData();
		}
		public List<HighScoreStruct> getHighScores()
		{
			
			return highscore;
			
		}
	}


}
